const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');

import Dialog from '../../../../../ucenter/lib/vant/dialog/dialog';

const app = getApp();

var knowledgetype_s = [{ "id": "1", "name": "高中及以下" }, { "id": "2", "name": "专科" }, { "id": "3", "name": "本科" }, { "id": "4", "name": "硕士" }, { "id": "5", "name": "博士及以上" }];

var trade_s = [{ "id": "1", "name": "未毕业" }, { "id": "2", "name": "互联网" }, { "id": "3", "name": "IT" }, { "id": "4", "name": "制造业" }, { "id": "5", "name": "金融" }, { "id": "6", "name": "教育" }, { "id": "7", "name": "医药医疗" }, { "id": "8", "name": "建筑房地产" }, { "id": "9", "name": "传媒" }, { "id": "10", "name": "政府机关" }, { "id": "11", "name": "服务" }, { "id": "12", "name": "我自己填" }];

var job_s = [{ "id": "1", "name": "学生" }, { "id": "2", "name": "创业者" }, { "id": "3", "name": "程序员" }, { "id": "4", "name": "工程师" }, { "id": "5", "name": "运营" }, { "id": "6", "name": "销售" }, { "id": "7", "name": "企业老板" }, { "id": "8", "name": "中高层管理者" }, { "id": "9", "name": "普通白领" }, { "id": "10", "name": "公务员" }, { "id": "11", "name": "事业单位员工" }, { "id": "12", "name": "我自己填" }];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    customlabel: '',
    custitle: '',
    selkind: '',
    logininfo: {},
    inputcount:0,
    visible2: false,
    actions2: [
      {
        name: '女',
      },
      {
        name: '男',
      }
    ],
    knowledgetype: knowledgetype_s,
    knowledgetype_index: '',
    tradetype: trade_s,
    tradetype_index: '',
    jobtype: job_s,
    jobtype_index: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
    });

    //计算店铺公告长度
    let content = this.data.logininfo.storeInfo.storeremark;
    if (content != null){
      let len = 50 - content.length;

      this.setData({
        inputcount: len
      });
    }
  },

  dostoreremark: function (event) {
    let content = event.detail.value;
    let len = 50 - content.length;

    this.setData({
      inputcount: len
    });
  },
  doupdatename: function (event) {
    var _this = this;

    let akind = event.currentTarget.dataset.name;
    let updatename = event.detail.value;

    let atitle = '';
    let param = {};
    if (akind == 'signname') {
      atitle = '修改个性签名';
      param = {
        "openid": app.globalData.logininfo.openid,
        "signname": updatename
      };
    }
    else if (akind == 'wechat') {
      atitle = '修改微信号';
      param = {
        "openid": app.globalData.logininfo.openid,
        "wechat": updatename
      };
    }

    wx.showLoading({
      title: atitle,
      //mask: true
    })

    util.post(api.My_fetchTokenUrl).then(function (res) {
      if (res.access_token != '') {
        return Promise.resolve({
          access_token: res.access_token
        });
      }
    }).then(function (res) {
      //console.log("res:" + res.accesstoken);

      if (res.access_token != '') {
        _this.setData({
          accesstoken: res.access_token
        });

        let url = api.My_doUpdateHomeInfo + "?access_token=" + _this.data.accesstoken;
        util.request(url, param, 'POST').then((res2) => {
          console.log(res2);

          if (akind == 'signname') {
            _this.data.logininfo.homeInfo.signname = updatename;
          }
          else if (akind == 'wechat') {
            _this.data.logininfo.homeInfo.wechat = updatename;
          }

          wx.setStorage({
            key: 'logininfo',
            data: _this.data.logininfo
          });

          wx.hideLoading();
        }).catch((err) => {
          wx.hideLoading();

          console.log(err);
          console.log("保存信息错误!");
        });
      }
      else {
        console.log("error accesstoken!");
      }
    }).catch((err) => {
      wx.hideLoading();

      console.log(err);
    });
  },
  selectChange: function (e) {
    var _this = this;
    var updatename = e.detail.value;
    let akind = e.currentTarget.dataset.name;
    if ('birthday' == akind) {
      var selname = 'birthday';
    } else {
      var selname = _this.data.tradetype[updatename].name;
    }
    if (selname == '我自己填' && akind == 'job') {
      this.setData({
        customlabel: '',
        custitle:'职业',
        selkind:'job',
        show: true,
      });

      //request api

      return;
    }
    else if (selname == '我自己填' && akind == 'trade') {
      this.setData({
        customlabel: '',
        custitle: '行业',
        selkind: 'trade',
        show: true,
      });

      //request api

      return;
    }

    //console.log(e);

    let atitle = '';
    let param = {};
    if (akind == 'birthday') {
      atitle = '修改出生日期';
      param = {
        "openid": app.globalData.logininfo.openid,
        "birthday": updatename
      };
    }
    else if (akind == 'knowledge') {
      atitle = '修改学历';
      param = {
        "openid": app.globalData.logininfo.openid,
        "knowledge": _this.data.knowledgetype[updatename].name
      };
    }
    else if (akind == 'trade') {
      atitle = '修改行业';
      param = {
        "openid": app.globalData.logininfo.openid,
        "trade": _this.data.tradetype[updatename].name
      };
    }
    else if (akind == 'job') {
      atitle = '修改职业';
      param = {
        "openid": app.globalData.logininfo.openid,
        "job": _this.data.jobtype[updatename].name
      };
    }

    wx.showLoading({
      title: atitle,
      //mask: true
    })

    util.post(api.My_fetchTokenUrl).then(function (res) {
      if (res.access_token != '') {
        return Promise.resolve({
          access_token: res.access_token
        });
      }
    }).then(function (res) {
      //console.log("res:" + res.accesstoken);

      if (res.access_token != '') {
        _this.setData({
          accesstoken: res.access_token
        });
        let url = api.My_doUpdateHomeInfo + "?access_token=" + _this.data.accesstoken;
        util.request(url, param, 'POST').then((res2) => {
          console.log(res2);

          if (akind == 'birthday') {
            _this.data.logininfo.homeInfo.birthday = updatename;
          }
          else if (akind == 'knowledge') {
            _this.data.logininfo.homeInfo.knowledge = _this.data.knowledgetype[updatename].name;
            //console.log(_this.data.knowledgetype[updatename]);
          }
          else if (akind == 'trade') {
            _this.data.logininfo.homeInfo.trade = _this.data.tradetype[updatename].name;
            //console.log(_this.data.knowledgetype[updatename]);
          }
          else if (akind == 'job') {
            _this.data.logininfo.homeInfo.job = _this.data.jobtype[updatename].name;
            //console.log(_this.data.knowledgetype[updatename]);
          }

          //解决控件数据刷新问题
          _this.setData({
            logininfo: _this.data.logininfo
          })

          wx.setStorage({
            key: 'logininfo',
            data: _this.data.logininfo
          });

          wx.hideLoading();
        }).catch((err) => {
          wx.hideLoading();

          console.log(err);
          console.log("保存信息错误!");
        });
      }
      else {
        console.log("error accesstoken!");
      }
    }).catch((err) => {
      wx.hideLoading();

      console.log(err);
    });
  },
  doSex(event) {
    this.setData({
      visible2: true
    });
  },
  doSheetCancel() {
    this.setData({
      visible2: false
    });
  },
  doSheetHandle({ detail }) {
    const index = detail.index;
    //console.log(index);
    var _this = this;
    var updatename = index;

    let atitle = '修改性别';
    let param = {
      "openid": app.globalData.logininfo.openid,
      "sex": updatename
    };

    wx.showLoading({
      title: atitle,
      //mask: true
    })

    util.post(api.My_fetchTokenUrl).then(function (res) {
      if (res.access_token != '') {
        return Promise.resolve({
          access_token: res.access_token
        });
      }
    }).then(function (res) {
      //console.log("res:" + res.accesstoken);

      if (res.access_token != '') {
        _this.setData({
          accesstoken: res.access_token
        });

        let url = api.My_doUpdateHomeInfo + "?access_token=" + _this.data.accesstoken;
        util.request(url, param, 'POST').then((res2) => {
          console.log(res2);
          _this.data.logininfo.homeInfo.sex = updatename;
          _this.data.logininfo.homeInfo.sexname = updatename==1?'男':'女';

          //解决控件数据刷新问题
          _this.setData({
            logininfo: _this.data.logininfo
          })

          wx.setStorage({
            key: 'logininfo',
            data: _this.data.logininfo
          });

          wx.hideLoading();
        }).catch((err) => {
          wx.hideLoading();

          console.log(err);
          console.log("保存信息错误!");
        });
      }
      else {
        console.log("error accesstoken!");
      }
    }).catch((err) => {
      wx.hideLoading();

      console.log(err);
    });

    this.setData({
      visible2: false,
    });
  },
  doaddrsetup: function () {
    wx.navigateTo({
      url: 'addrsetup/addrsetup?userid=1',
    })
  },
  // 进入主页隐私设置
  onUserInfoClick: function () {
    wx.navigateTo({
      url: 'yinsi/yinsi?userid=1',
    })
  },
  // 进入圈主介绍设置
  onUserInfointro: function(){
    wx.navigateTo({
      url: 'intro/intro?userid=1',
    })
  },
  dointeresttag: function () {
    wx.navigateTo({
      url: 'interesttag/interesttag?userid=1',
    })
  },
  onCustomChange(event) {
    // event.detail 为当前输入的值
    /*console.log(event.detail);*/


  },
  onCustomblur(event) {
    // event.detail 为当前输入的值
    //console.log(event.detail.value);

    /*if (event.detail.value.length > 6) {
      this.setData({
        customlabel: event.detail.value.substring(0, 6),
      });
      wx.showToast({
        title: '只能输入6个字符!',
        icon: 'success',
        duration: 2000
      })

      return;
    }*/

    this.setData({
      //tags: this.data.tags,
      customlabel: event.detail.value,
    });
  },
  onCustomClose(event) {
    var _this = this

    if (event.detail === 'confirm') {
      //console.log(event);

      if (_this.data.customlabel != '') {
        
        // 异步关闭弹窗
        setTimeout(() => {
          
          //***************
          let atitle = '';
          let param = {};
          if (_this.data.selkind == 'trade') {
            atitle = '修改行业';
            param = {
              "openid": app.globalData.logininfo.openid,
              "trade": _this.data.customlabel
            };
          }
          else if (_this.data.selkind == 'job') {
            atitle = '修改职业';
            param = {
              "openid": app.globalData.logininfo.openid,
              "job": _this.data.customlabel
            };
          }

          wx.showLoading({
            title: atitle,
            //mask: true
          })

          util.post(api.My_fetchTokenUrl).then(function (res) {
            if (res.access_token != '') {
              return Promise.resolve({
                access_token: res.access_token
              });
            }
          }).then(function (res) {
            //console.log("res:" + res.accesstoken);

            if (res.access_token != '') {
              _this.setData({
                accesstoken: res.access_token
              });

              let url = api.My_doUpdateHomeInfo + "?access_token=" + _this.data.accesstoken;
              util.request(url, param, 'POST').then((res2) => {
                //console.log(res2);

                if (_this.data.selkind == 'trade') {
                  _this.data.logininfo.homeInfo.trade = _this.data.customlabel;
                  //console.log(_this.data.knowledgetype[updatename]);
                }
                else if (_this.data.selkind == 'job') {
                  _this.data.logininfo.homeInfo.job = _this.data.customlabel;
                  //console.log(_this.data.knowledgetype[updatename]);
                }

                //解决控件数据刷新问题
                _this.setData({
                  logininfo: _this.data.logininfo
                })

                wx.setStorage({
                  key: 'logininfo',
                  data: _this.data.logininfo
                });

                wx.hideLoading();
              }).catch((err) => {
                wx.hideLoading();

                console.log(err);
                console.log("保存信息错误!");
              });
            }
            else {
              console.log("error accesstoken!");
            }
          }).catch((err) => {
            wx.hideLoading();

            console.log(err);
          });
          //**********************

          this.setData({
            show: false
          });
        }, 100);
      }
      else {
        this.setData({
          show: false
        });
      }
    } else {
      this.setData({
        show: false
      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 跳转身份认证
   */
  gotoAuthentication: function () {
    wx.navigateTo({
      url: '/grade/pages/grade/authentication/authentication',
    })
  },
})